package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.DishDao;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishDto;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Override
    @Transactional
    public void add(DishDto dishDto) {
        List<DishFlavor> flavors = dishDto.getFlavors();
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDto,dish);
        dishService.save(dish);

        String name1 = dishDto.getName();



        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(name1!=null,Dish::getName,name1);
        Dish dish2 = dishService.getOne(wrapper);

        for (DishFlavor flavor : flavors) {
//            Long dishId = dishDto.getId();
            Long dishId = dish2.getId();
            DishFlavor dishFlavor=new DishFlavor();
            String name = flavor.getName();
            String value = flavor.getValue();
//            flavor.setDishId(dishId);
            dishFlavor.setDishId(dishId);
            dishFlavor.setValue(value);
            dishFlavor.setName(name);
            System.out.println(dishFlavor);
            dishFlavorService.save(dishFlavor);
        }




    }


}
